import json
import urllib.request
import ssl
from urllib.parse import quote
import string

from utils.expressage_config import get_key

host = 'https://wuliu.market.alicloudapi.com'
path = '/kdi'
method = 'GET'
appcode = '9247d06a88f649bd80fdb23450d696f0'


# querys = 'no=3824304315669&type=YUNDA'
# querys = 'no=3824304315669'
# bodys = {}


def execute_data_query(ExpressNumber, ExpressCompany):
    expressage_type = get_key(ExpressCompany)
    if expressage_type == None:
        querys = 'no=' + ExpressNumber
    else:
        querys = 'no=' + ExpressNumber + '&type=' + expressage_type
    url = host + path + '?' + querys
    newurl = quote(url, safe=string.printable)
    request = urllib.request.Request(newurl)
    request.add_header('Authorization', 'APPCODE ' + appcode)
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    response = urllib.request.urlopen(request, context=ctx)
    content = response.read()
    if content: content = content.decode('UTF-8')
    content = json.loads(content)
    return content


if __name__ == '__main__':
    execute_data_query('3824304315669', '韵达')
